#include "favoritemanagerwindow.h"
#include "ui_favoritemanagerwindow.h"

FavoriteManagerWindow::FavoriteManagerWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FavoriteManagerWindow)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_StyledBackground);
//    qDebug() << parent->width() / 2 << parent->height() / 2;
//    resize(parent->width() / 2, parent->height() / 2);
    // 初始化时加载数据
    loadData();
    updateWindow();

}

FavoriteManagerWindow::~FavoriteManagerWindow()
{
    delete ui;
}

void FavoriteManagerWindow::paintEvent(QPaintEvent *event)
{
    // 调整大小
    QWidget *pa = dynamic_cast<QWidget*>(parent());
    resize(pa->width() / 2, pa->height() / 2);
    QPoint p = pa->pos();
    QSize s = pa->size();
    move(s.width() / 2 - size().width() / 2, s.height() / 5);
}

void FavoriteManagerWindow::addFavoriteUrlLink(UrlInfo urlInfo)
{
    // 存储数据到内存
    this->urlInfos.push_back(urlInfo);
    // 数据持久化

    // 刷新界面
    updateWindow();
}

void FavoriteManagerWindow::deleteFavoriteUrlLink(QString link)
{
    qDebug() << "delete: " << link;
    // 删除对应链接内存数据
    std::vector<UrlInfo> t;
    for(int i = 0; i < urlInfos.size(); i++){
        if(urlInfos[i].url == link.toStdString())continue;
        t.push_back(urlInfos[i]);
    }
    // 删除持久层数据

    // 更新组件数据及刷新界面
    this->urlInfos = t;
    updateWindow();
}

void FavoriteManagerWindow::loadData()
{
    // TODO，加载持久层数据

    // 存储至应用内存中
    // 测试数据
    urlInfos.push_back(UrlInfo("百度一下，你就知道", "", "https://www.baidu.com"));
    urlInfos.push_back(UrlInfo("哔哩哔哩 (゜-゜)つロ 干杯~-bilibili", "", "https://www.bilibili.com"));
}

void FavoriteManagerWindow::updateWindow()
{
    // 绘制界面
    QListWidget *listWidget = this->ui->listWidget;
    listWidget->clear();
    for(int i = 0; i < urlInfos.size(); i++){
        QListWidgetItem *widgetItem = new QListWidgetItem();
        UrlInfoItemWidget *item = new UrlInfoItemWidget();
        connect(item, &UrlInfoItemWidget::deleteItem, this, &FavoriteManagerWindow::deleteFavoriteUrlLink);
        item->setUrlInfo(urlInfos[i]);
        listWidget->addItem(widgetItem);
        listWidget->setItemWidget(widgetItem, item);
    }
}

void FavoriteManagerWindow::closeEvent(QCloseEvent *event)
{
    // 自定窗口关闭事件
    qDebug() << "自定义窗口关闭事件";
    //    hide();
}

void FavoriteManagerWindow::updateData()
{
    QListWidget *listWidget = this->ui->listWidget;
    int cnt = listWidget->count();
    std::vector<UrlInfo> t;
    for(int i = 0; i < cnt; i++){
        QListWidgetItem *listWidgetItem = listWidget->item(i);
        UrlInfoItemWidget *itemWidget = (UrlInfoItemWidget*)listWidget->itemWidget(listWidgetItem);
        t.push_back(itemWidget->getUrlInfo());
    }
    this->urlInfos = t;

    // 数据持久化

    // 消息提示
    NotifyManager *manager = new NotifyManager(nullptr);
    manager->setMaxCount(5);
    manager->setDisplayTime(2000);
    manager->setNotifyWndSize(300, 80);
//    manager->setStyleSheet("#notify-background {"
//                           "background: black;"
//                           "}"
//                           "#notify-title{"
//                           "font: bold 14px 黑体;"
//                           "color: #eeeeee;"
//                           "}"
//                           "#notify-body{"
//                           "font: 12px 黑体;"
//                           "color: #dddddd;"
//                           "}"
//                           "#notify-close-btn{ "
//                           "border: 0;"
//                           "color: #999999;"
//                           "}"
//                           "#notify-close-btn:hover{ "
//                           "background: #444444;"
//                           "}", "black");
   manager->notify("普通信息", "更新完成");
}



void FavoriteManagerWindow::on_pushButton_4_clicked()
{
    hide();
}


void FavoriteManagerWindow::on_pushButton_5_clicked()
{
    this->updateData();
}

